/*
Simple OpenID Plugin
http://code.google.com/p/openid-selector/

This code is licenced under the New BSD License.
*/

var providers = {
	google: {
        name: 'Google',
        url: ''
    },
    yandex: {
    	name: 'Yandex',
		url: 'http://openid.yandex.ru/'
    },
    rambler: {
    	name: 'Rambler',
    	label: 'Введите ваш Rambler аккаунт',
    	url: 'http://id.rambler.ru/users/{username}/'
    },
    livejournal: {
        name: 'LiveJournal',
        label: 'Введите ваш Livejournal аккаунт.',
        url: 'http://{username}.livejournal.com/'
    },
    blogger: {
        name: 'Blogger',
        label: 'Введите ваш Blogger аккаунт',
        url: 'http://{username}.blogspot.com/'
    },
    myopenid: {
        name: 'MyOpenID',
        label: 'Введите ваш MyOpenID аккаунт.',
        url: 'http://{username}.myopenid.com/'
    },
    flickr: {
        name: 'Flickr',        
        label: 'Введите ваш Flickr аккаунт.',
        url: 'http://flickr.com/{username}/'
    },
    wordpress: {
        name: 'Wordpress',
        label: 'Введите ваш Wordpress.com аккаунт.',
        url: 'http://{username}.wordpress.com/'
    },
    openid: {
        name: 'OpenID',
        image: 'openid',     
        label: 'Используйте свой OpenID.',
        url: null
    }
    
};
var openid = {
	img_path: '/static/images/openid/',
	custom_open_id: 'custom_openid',
	provider: null,
    init: function() {
        $('#openid_form').submit(this.submit);

        var openid_btns = $('#openid_btns');
        ul = $('<ul/>').appendTo(openid_btns);
        for (id in providers) {
        	li = $('<li/>').appendTo(ul);
           	li.append(this.getBoxHTML(providers[id], 'small', '.png'));
           	li.attr('id', providers[id]["name"].toLowerCase());
        }
    },
    
    getBoxHTML: function(provider, box_size, image_ext) {
        var box_id = provider["name"].toLowerCase();
        image = provider["image"];
        image = image ? image : box_id;
        return '<a title="'+provider["name"]+'" href="javascript: openid.signin(\''+ box_id +'\');"' +
        		' ><img src="' + this.img_path + image + image_ext+'"/> ' + provider["name"] + ' </a>';    
    
    },

    signin: function(box_id) {
    	var provider = providers[box_id];
  		if (! provider) {
  			return;
  		}
		
		openid.provider = provider;	
		if (provider['label']) {
			this.useInputBox(provider);
		} else {
			$('#openid_form').submit();
		}
    },

    submit: function() {
    	var url = openid.provider['url']; 
    	if (url) {
    		var username = $('#openid_username').val();
    		url = url.replace('{username}', username);
    	} else {
    		url = $('#' + openid.custom_open_id).val();
    	}
		openid.setOpenIdUrl(url);
    	return true;
    },
    
    setOpenIdUrl: function(url) {
    	$('#openid_url').val(url);
    },

    useInputBox: function (provider) {
		var input_area = $('#' + provider['name'].toLowerCase());
		
		var html = '';
		var id = 'openid_username';
		var value = '';
		var label = provider['label'];
		var style = '';
		
		html = '<div id="input_fields">';
		if (label) {
			html += '<p>' + label + '</p>';
		}
		if (provider['name'] == 'OpenID') {
			id = openid.custom_open_id;
			value = 'http://';
			style = 'background:#FFF url('+this.img_path+'openid-inputicon.gif) no-repeat scroll 0 50%; padding-left:18px;';
		}
		html += '<input class="field" id="'+id+'" type="text" style="'+style+'" name="'+id+'" value="'+value+'" />' + 
					'<input class="signIn" id="openid_submit" type="submit" value="Войти"/>';
		html += '</div';
		$('#input_fields').remove();
		input_area.append(html);

		$('#'+id).focus();
    }
};
